local skel = fk.CreateSkill {
  name = "emo__huayuan",
  tags = {Skill.Switch},
  attached_skill_name = "emo__huayuan&",
}

Fk:loadTranslationTable{
  ["emo__huayuan"] = "画渊",
  [":emo__huayuan"] = "转换技，其他角色可以将一张装备牌当：阳：回复+1的【桃】；阴：伤害+1的【杀】对你使用。",

  [":emo__huayuan_yang"] = "转换技，其他角色可以将一张装备牌当：<font color='#E0DB2F'>阳：回复+1的【桃】；</font><font color='grey'>阴：伤害+1的【杀】</font>对你使用。",
  [":emo__huayuan_yin"] = "转换技，其他角色可以将一张装备牌当：<font color='grey'>阳：回复+1的【桃】；</font><font color='#E0DB2F'>阴：伤害+1的【杀】</font>对你使用。",

  ["#emo__huayuan-use"] = "画渊：你可以将一张装备牌当【%arg】对%src使用",
  ["emo__huayuan&"] = "画渊",
  [":emo__huayuan&"] = "转换技，你可以将一张装备牌当：阳：回复+1的【桃】；阴：伤害+1的【杀】对拥有“画渊”的角色使用。",

  ["$EmoUseOtherSkill"] = "%from 发动了 %to 的技能 %arg",

  ["$emo__huayuan1"] = "",
  ["$emo__huayuan2"] = "",
}

-- 仅用于转换状态
skel:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and data.tos and table.contains(data.tos, player) and table.contains(data.card.skillNames, skel.name) then
      local extra_data = (data.extra_data or {})[skel.name] or {}
      return extra_data[1] == player and extra_data[2] == player:getSwitchSkillState(skel.name)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = Util.DummyFunc,
})

local skel2 = fk.CreateSkill {
  name = "emo__huayuan&",
}

skel2:addEffect("viewas", {
  pattern = "slash,peach",
  mute = true,
  mute_card = false,
  prompt = function (self, player)
    local other = table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and p:hasSkill(skel.name)
    end)
    if not other then return " " end
    local state = other:getSwitchSkillState(skel.name)
    local name = state == fk.SwitchYang and "peach" or "slash"
    return "#emo__huayuan-use:" .. other.id .. "::" .. name
  end,
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  interaction = function(self, player) -- 防止场上存在两个阴阳状态不同的陆机……
    local choices = {}
    for _, c in ipairs(Fk:currentRoom().alive_players) do
      if c:hasSkill(skel.name) and c ~= player then
        local state = c:getSwitchSkillState(skel.name)
        local name = state == fk.SwitchYang and "peach" or "slash"
        table.insertIfNeed(choices, name)
      end
    end
    if #choices > 0 then
      return UI.CardNameBox { choices = choices }
    end
  end,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  on_use = function(self, room, skillData, card, params)
    local to = skillData.tos[1]
    local use = { ---@type UseCardDataSpec
      from = skillData.from,
      tos = skillData.tos,
      card = card,
    }
    if not to then -- 注意，濒死求桃使用没目标
      if params and params.extra_data and params.extra_data.must_targets then
        to = room:getPlayerById(params.extra_data.must_targets[1])
        if not to then return end
        use.tos = {to}
      end
    end
    use.extra_data = use.extra_data or {}
    use.extra_data[skel.name] = {to, to:getSwitchSkillState(skel.name)}
    if card.trueName == "slash" then
      use.additionalDamage = (use.additionalDamage or 0) + 1
    else
      use.additionalRecover = (use.additionalRecover or 0) + 1
    end
    room:sendLog{
      from = skillData.from.id, to = {to.id}, type = "$EmoUseOtherSkill"
    }
    return use
  end,
  enabled_at_play = function(self, player)
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:hasSkill(skel.name) and p ~= player then
        local state = p:getSwitchSkillState(skel.name)
        if state == fk.SwitchYin then
          return true
        end
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    if not response and Fk.currentResponsePattern then
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p:hasSkill(skel.name) and p ~= player then
          local state = p:getSwitchSkillState(skel.name)
          local name = state == fk.SwitchYang and "peach" or "slash"
          if Exppattern:Parse(Fk.currentResponsePattern):matchExp(name) then
            return true
          end
        end
      end
    end
  end,
})

skel2:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    if from and to and card and table.contains(card.skillNames, skel.name) then
      if not to:hasSkill(skel.name) then
        return true
      end
      local state = to:getSwitchSkillState(skel.name)
      local name = state == fk.SwitchYang and "peach" or "slash"
      return card.trueName ~= name
    end
  end,
})

skel2:addAI(nil, "vs_skill")

return {skel, skel2}
